/*******************************************
* Zeus CMD - OpenGL ES Tutorial 11 : Depth *
* By Grant James (ZEUS)                    *
* http://www.zeuscmd.com                   *
*******************************************/
//#pragma comment(lib, "libGLES_CM.lib")

//#define GLUTES_STATIC

#include <glut.h>

GLfloat triangle[] = {
	 0.5f,-0.5f, 0.0f,
	 0.0f, 0.5f, 0.0f,
	-0.5f,-0.5f, 0.0f
};

GLfloat colors[] = {
	1.0f, 0.0f, 0.0f, 1.0f,
	0.0f, 1.0f, 0.0f, 1.0f,
	0.0f, 0.0f, 1.0f, 1.0f
};

void init()
{
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0f);

	glVertexPointer(3, GL_FLOAT, 0, triangle);
	glColorPointer(4, GL_FLOAT, 0, colors);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
}

void display()
{
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	glDrawArrays(GL_TRIANGLES, 0, 3);

	glPushMatrix();
	{
		glTranslatef(-0.2f, 0.0f, -1.0f);

		glDrawArrays(GL_TRIANGLES, 0, 3);

		glRotatef(45.0f, 0.0f, 0.0f, 1.0f);

		glDrawArrays(GL_TRIANGLES, 0, 3);
	}
	glPopMatrix();

	glPushMatrix();
	{
		glTranslatef(0.5f, 0.0f, 0.0f);
		glScalef(0.5f, 0.5f, 0.5f);

		glDrawArrays(GL_TRIANGLES, 0, 3);
	}
	glPopMatrix();
	// Test with triangle
	glPushMatrix();
	{
		glTranslatef(-0.6f, 0.5f, -20.0f);
		glScalef(0.4f, 0.5f, 0.5f);

		glDrawArrays(GL_TRIANGLES, 0, 3);
	}
	glPopMatrix();

	glFlush ();
	glutSwapBuffers();
}

void keyboard2(int key, int x, int y)
{
	switch(key)
	{
	case GLUT_KEY_UP : exit(0); break;
	}
}

void reshape(int width, int height)
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glViewport(0, 0, width, height);
	glOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 20.0f);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void menu(int entry)
{
	switch(entry)
	{
	case 1 : exit(0); break;
	case 2 :
		if (glIsEnabled(GL_DEPTH_TEST))
			glDisable(GL_DEPTH_TEST);
		else
			glEnable(GL_DEPTH_TEST);
		
		glutPostRedisplay();
		break;
	}
}

int main(int argc, char *argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);

	glutCreateWindow("11 - Depth");

	init();

	glutDisplayFunc(display);
	glutReshapeFunc(reshape);
	glutSpecialFunc(keyboard2);

	glutCreateMenu(menu);
	glutAddMenuEntry("Depth", 2);
	glutAddMenuEntry("Quit", 1);

	glutAttachMenu(GLUT_LEFT_BUTTON);

	glutMainLoop();

	return 0;
}
